<template>
  <!-- 法务案件管理-待分配-页面 -->
  <section class="page legal-waiting-collect-case-manage">
    <data-form :model="model" @onSearch="refreshData" @handleResetForm="handleResetForm" :page="pageService">
      <template slot="inputs">
        <el-form-item label="订单号" prop="caseNumber">
          <el-input v-model="model.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="批次号" prop="batchNumber">
          <el-input v-model="model.batchNumber"></el-input>
        </el-form-item>
        <el-form-item label="委托方" prop="principalId">
          <el-select v-model="model.principalId" placeholder="请选择" clearable>
            <el-option v-for="{index,name,id} in principalList.content" :key="index" :label="name" :value="id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model="model.personalName"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="model.idCard"></el-input>
        </el-form-item>
        <el-form-item label="放款日期" prop="loanDate">
          <el-date-picker type="daterange" :editable="false" v-model="model.loanDate" placeholder="选择日期时间" clearable></el-date-picker>
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNo">
          <el-input v-model="model.mobileNo"></el-input>
        </el-form-item>
        <el-form-item label="业务团队" prop="partnerName">
          <el-input v-model="model.partnerName"></el-input>
        </el-form-item>
        <el-form-item label="贷款银行" prop="bankName">
          <el-input v-model="model.bankName"></el-input>
        </el-form-item>
        <el-form-item label="催收总金额" prop="overdueAmount">
          <number-range v-model="model.overdueAmount"></number-range>
        </el-form-item>
        <el-form-item label="业务承接地" prop="businessAddress">
          <el-input v-model="model.businessAddress"></el-input>
        </el-form-item>
        <el-form-item label="上牌地" prop="boardAddress">
          <el-input v-model="model.boardAddress"></el-input>
        </el-form-item>
        <el-form-item label="车牌号" prop="carNo">
          <el-input v-model="model.carNo"></el-input>
        </el-form-item>
        <el-form-item label="案件来源" prop="dataFromSource">
          <el-select v-model="model.dataFromSource" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('DataFromSource')" :key="index" :label="name" :value="code"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门催收剩余天数" prop="partCollectionRemainingDays">
          <number-range v-model="model.partCollectionRemainingDays" :isInteger="true"></number-range>
        </el-form-item>
        <el-form-item label="案件流入时间" prop="followInTime">
          <el-date-picker type="datetimerange" :editable="false" v-model="model.followInTime" placeholder="选择日期时间" clearable></el-date-picker>
        </el-form-item>
      </template>
    </data-form>
    <data-box :data="dataSet" :selectionList.sync="selectionList" @onPageChange="refreshData" :sort="sortService" :page="pageService" ref="data-box">
      <template slot="buttons">
        <el-button @click="manualDivision" v-auth="308">分案</el-button>
        <el-button @click="dealManualPartReturnCase" v-auth="273">委托方回收</el-button>
      </template>
      <template slot="columns">
        <el-table-column prop="caseNumber" label="订单编号" :min-width="$helper.getColumnWidth(6)">
          <template slot-scope="scope">
            <el-button type="text" @click="openExecutionPage(scope.row)">{{scope.row.caseNumber}}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="batchNumber" label="批次号" :min-width="$helper.getColumnWidth(6)"></el-table-column>
        <el-table-column prop="principalName" label="委托方" :min-width="$helper.getColumnWidth(4)"></el-table-column>
        <el-table-column prop="personalName" label="客户姓名" :min-width="$helper.getColumnWidth(3)"></el-table-column>
        <el-table-column prop="idCard" label="身份证号" :min-width="$helper.getColumnWidth(6)"></el-table-column>
        <el-table-column prop="loanDate" label="放款日期" :formatter="(row) => $filter.dateFormat(row.loanDate)" :min-width="$helper.getColumnWidth(5)"></el-table-column>
        <el-table-column prop="mobileNo" label="手机号" :min-width="$helper.getColumnWidth(4)"></el-table-column>
        <el-table-column prop="partnerName" label="业务团队" :min-width="$helper.getColumnWidth(4)"></el-table-column>
        <el-table-column prop="bankName" label="贷款银行" :min-width="$helper.getColumnWidth(4)"></el-table-column>
        <el-table-column prop="partCollectionRemainingDays" label="部门催收剩余天数" :min-width="$helper.getColumnWidth(3)"></el-table-column>
        <el-table-column prop="loanAmount" label="贷款金额" :min-width="$helper.getColumnWidth(4)" :formatter="(row) =>  $filter.toThousands(row.loanAmount)"></el-table-column>
        <el-table-column prop="overdueStage" label="逾期阶段" :min-width="$helper.getColumnWidth(4)"></el-table-column>
        <el-table-column prop="overdueDays" label="逾期天数" :min-width="$helper.getColumnWidth(3)"></el-table-column>
        <el-table-column prop="sumMoney" label="催收总金额" :min-width="$helper.getColumnWidth(6)" :formatter="(row) =>  $filter.toThousands(row.overdueAmount)"></el-table-column>
        <el-table-column prop="businessAddress" label="业务承接地" :min-width="$helper.getColumnWidth(4)"></el-table-column>
        <el-table-column prop="boardAddress" label="上牌地" :min-width="$helper.getColumnWidth(3)"></el-table-column>
        <el-table-column prop="carNo" label="车牌号" :min-width="$helper.getColumnWidth(3)"></el-table-column>
        <el-table-column prop="dataFromSource" label="案件来源" :formatter="(row) => $filter.dictConvert(row.dataFromSource,'DataFromSource')" :min-width="$helper.getColumnWidth(4)"></el-table-column>
        <el-table-column prop="followInTime" label="案件流入时间" :formatter="(row) => $filter.dateTimeFormat(row.followInTime)" :min-width="$helper.getColumnWidth(5)"></el-table-column>
      </template>
    </data-box>

    <el-dialog title="分案" :visible.sync="dialog.ManualDivision" :show-close="false" width="1000px" :close-on-click-modal="false" :close-on-press-escape="false" @open="$nextTick(()=>$refs['case-allot'].refreshDept())">
      <case-allot :fromChannel="fromChanel" @refreshList="refreshData" @close="dialog.ManualDivision=false" ref="case-allot"></case-allot>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false" title="委托方回收" :visible.sync="dialog.principalRecovery" width="400px">
      <principal-recovery :ids="selectedCaseIds" @close="dialog.principalRecovery = false;refreshData();" />
    </el-dialog>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Dependencies, Layout, Auth } from "~/core/decorator";
import { PageService } from "~/utils/page.service";
import { SortService } from "~/utils/sort.service";
import { Getter, Mutation, namespace, State } from "vuex-class";
import NumberRange from "~/components/common/number-range.vue";
import { TelCaseManageService } from "~/services/domain-service/tel-case-manage.service";
import { LegalCaseManageService } from "~/services/domain-service/legal-case-manage.service";
import CaseAllot from "~/components/distribution-common/case-allot.vue";
import { CASE_CHANNEL, MENU_CHANNEL } from "~/config/enum.config";
import { LabelingStrategyService } from "~/services/dataimp-service/labeling-strategy-service";
import GetLabelName from "~/components/labeling-strategy/get-label-name.vue";
import PrincipalRecovery from "~/components/apply-component/principal-recovery.vue";

const AllotModule = namespace("case-allot");
const CollectionExecutionModule = namespace("collection-execution");

@Auth(301)
@Layout("workspace")
@Component({
  name: "legal-waiting-collect-case-manage",
  components: {
    NumberRange,
    CaseAllot,
    GetLabelName,
    PrincipalRecovery
  }
})
export default class LegalWaitingCollectCaseManage extends Vue {
  @Dependencies(PageService)
  private pageService: PageService;
  @Dependencies(SortService)
  private sortService: SortService;
  @Dependencies(LabelingStrategyService)
  private labelingStrategyService: LabelingStrategyService;
  @Dependencies(LegalCaseManageService)
  private legallyCaseManageServiceTestJ: LegalCaseManageService;
  @Dependencies(TelCaseManageService)
  private telCaseManageService: TelCaseManageService;
  @Mutation private updateCollectionType;
  @Getter private callDepartmentData;
  @AllotModule.Mutation private updateCaseIds;
  @State private principalList: any;


  private fromChanel = CASE_CHANNEL.legal;
  private model: any = {
    caseNumber: "",
    personalName: "",
    mobileNo: "",
    idCard: "",
    overdueAmount: {
      max: "",
      min: ""
    },
    loanDate: [],
    principalId: '',
    partCollectionRemainingDays: { min: '', max: '' },
    bankName: '',
    businessAddress: '',
    boardAddress: '',
    carNo: '',
    dataFromSource: '',
    followInTime: [],
    partnerName: '',
    batchNumber: ''
  };
  private dataSet: any[] = [];
  private selectionList: any[] = [];

  private options: any = [];

  private dialog: any = {
    ManualDivision: false,
    principalRecovery: false
  };

  private created() {
    this.refreshData();
  }


  private paramsHandler() {
    const params = { ...this.model }
    params.overdueAmountMax = this.model.overdueAmount ? this.model.overdueAmount.max : ""
    params.overdueAmountMin = this.model.overdueAmount ? this.model.overdueAmount.min : ""
    params.partCollectionRemainingDaysMax = params.partCollectionRemainingDays ? this.model.partCollectionRemainingDays.max : ""
    params.partCollectionRemainingDaysMin = params.partCollectionRemainingDays ? this.model.partCollectionRemainingDays.min : ""
    params.followInTimeMin = this.model.followInTime.length ? this.$filter.dateTimeFormat(this.model.followInTime[0]) : ''
    params.followInTimeMax = this.model.followInTime.length ? this.$filter.dateTimeFormat(this.model.followInTime[1]) : ''
    params.loanDateMin = this.$filter.dateRanageFormat(this.model.loanDate).start
    params.loanDateMax = this.$filter.dateRanageFormat(this.model.loanDate).end

    delete params.overdueAmount
    delete params.partCollectionRemainingDays
    delete params.followInTime
    delete params.loanDate

    return params
  }

  private get selectedCaseIds() {
    return this.selectionList.map(v => v.id);
  }

  private refreshData() {
    this.legallyCaseManageServiceTestJ
      .waitDistribution(this.paramsHandler(), this.pageService, this.sortService)
      .subscribe(data => {
        this.dataSet = data;
        this.selectionList = [];
      });
  }

  /**
   * 范围输入框的重置
   */
  private handleResetForm() {
    this.model.overdueAmount = { min: "", max: "" };
    this.model.partCollectionRemainingDays = { max: "", min: "" };
  }

  /**
   * 手动分案
   */
  private manualDivision() {
    if (this.selectionList.length === 0) {
      this.$message.info("请选择要分配的案件");
      return;
    }
    this.updateCaseIds(this.selectionList.map(v => v.id));
    this.dialog.ManualDivision = true;
  }

  /**
   * 进入查看执行页
   */
  private openExecutionPage(row) {
    this.$router.openCaseDetail(row.id, {
      menuChannel: MENU_CHANNEL.Legal_await,
      hiddenCaseList: true,
      hiddenAction: true
    });
  }
  /**
   * 委托方回收
   */
  private dealManualPartReturnCase() {
    if (this.selectionList.length !== 0) {
      this.dialog.principalRecovery = true;
    } else {
      this.$message(`请选择要进行委托方回收的案件`);
    }
  }
}
</script>

<style lang="less" scoped>
</style>
